<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>某销售订单的收款记录</title>
    <script type="text/javascript">
      var newReceivableWin;
      var editReceivableWin;
      var receivableId;
      Ext.onReady(function(){
        receivablesForSaleDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'Receivable',
          proxy : {
            type : 'ajax',
            extraParams:{id:'<%=params[:id]%>'},
            url : '/sale_order/getReceivablesBySaleId',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          autoLoad : true
        });

        //生成要订单的产品的Grid
        var receivableForSaleGrid = Ext.create('Ext.grid.Panel', {
          //title:'收款记录列表',
          store: receivablesForSaleDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{              
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '预收单号',
              dataIndex: 'order_number',
              width: 110
            },{
              header: '收款金额',
              align: 'right',
              dataIndex: 'amount',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 75
            },{
              header: '支付方式',
              dataIndex: 'type_of_payment',
              width: 70
            },{
              header: '收款人',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '收款日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 70
            },{
              header: '描述',
              dataIndex: 'description',
              width: 180
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 150
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 100
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: 1085,
          height: 440,
          tbar: [{
              text: '新增',
              iconCls: 'addItem',
              handler : function(){
                newReceivableWin = Ext.create('Ext.window.Window', {
                  layout: 'fit',
                  width: 567,
                  height: 165,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '新增收款记录',
                  autoLoad: {
                    url: "/receivables/newReceivable?id=<%=params[:id]%>",
                    scripts: true
                  }
                });
                newReceivableWin.on("destroy",function(){
                  receivablesForSaleDS.load();
                });
                newReceivableWin.resizable = false;
                newReceivableWin.show();
              }
            },"-",{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = receivableForSaleGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  if(records[0].get('auditing_status').indexOf('通过') < 0 && records[0].get('auditing_status').indexOf('撤消') < 0 && records[0].get('auditing_status').indexOf('申请') < 0){
                    receivableId = records[0].get('id');
                    saleOrderId = records[0].get('sale_order_id');
                    editReceivableWin = Ext.create('Ext.window.Window', {
                      layout: 'fit',
                      width: 567,
                      height: 165,
                      closeAction: 'destroy',
                      plain: true,
                      modal: true,
                      constrain:true,
                      //modal: true,
                      title: '编辑收款订单',
                      autoLoad: {
                        url: "receivables/editReceivable?id="+receivableId+"&sale_order_id="+saleOrderId,
                        scripts: true,
                        callback: function(options, success, response, request)
                        {
                          if (response.responseText === "NoPrivilege"){
                            editReceivableWin.close();
                          }
                        }
                      }
                    });
                    editReceivableWin.on("destroy",function(){
                      receivablesForSaleDS.load();
                    });
                    editReceivableWin.resizable = false;
                    editReceivableWin.show();
                  }
                }
              }
            },'-',{
              text: '撤消',
              iconCls: 'cancel',
              handler : function(){
                var records = receivableForSaleGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('auditing_status') != '通过撤消' && records[0].get('auditing_status').indexOf('申请') < 0){
                    receivableId = records[0].get('id');
                    Ext.MessageBox.confirm("提示","你确认要申请撤消此收款记录吗？",function(btn){
                      if(btn=="yes"){
                        Ext.Ajax.request({
                          url : '/receivables/applyToCancel?id=' + receivableId,
                          success : function (result, request) {
                            receivablesForSaleDS.load();
                          },
                          failure : function (result, request){
                            Ext.MessageBox.show({
                              title: '消息',
                              msg: "通讯失败，请从新操作",
                              buttons: Ext.MessageBox.OK,
                              icon: Ext.MessageBox.WARNING
                            });
                          },
                          method : 'GET'
                        });
                      }else{
                        //alert("no");
                      }
                    });
                  }
                }
              }
            }],
          frame: true
        });
        
        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: 1090,
          height: 590,
          frame:false,          
          layout: {type: 'table',columns:1},          
          items:[receivableForSaleGrid]
        });
        workbenchPanel.render("receivables_for_sale_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="receivables_for_sale_workbench_panel"></div>
  </body>
</html>